package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LevelPrivilegeServiceImpl implements LevelPrivilegeService {
    @Autowired
    LevelConverter levelConverter;
    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    // 分页查询(后台等级权限管理-特权管理)
    @Override
    public PageDTO<LevelPrivilegeDTO> getLevelPrivilegeList(LevelPrivilegePageRequest pageRequest) {
//        处理没有任何关键字时，用分页查
        Integer pageNum = pageRequest.getPageNum() != null ? pageRequest.getPageNum() : 1;
        Integer pageSize = pageRequest.getCount() != null ? pageRequest.getCount() : 10;
//        根据权限吗和权限名称采用模糊查询
        QueryWrapper<LevelPrivilegeDO> queryWrapper = new QueryWrapper<>();
        if (pageRequest.getKeyword() != null && pageRequest.getKeyword() != "") {
            queryWrapper.like("privilege_code", pageRequest.getKeyword())
                    .or()
                    .like("privilege_name", pageRequest.getKeyword());
        } else {
            queryWrapper = null;
        }
        Page<LevelPrivilegeDO> levelPrivilegeDOPage = new Page<>(pageNum, pageSize);
        Page<LevelPrivilegeDO> levelPrivilegeDOPageContent = levelPrivilegeMapper.selectPage(levelPrivilegeDOPage, queryWrapper);
        //        计算查询出的总条数
        long totalCount = levelPrivilegeDOPageContent.getTotal();
        Integer total = (int) totalCount;
//        Integer totalCount = levelPrivilegeDOPageContent.size();
        List<LevelPrivilegeDO> levelPrivilegeDOlist = levelPrivilegeDOPageContent.getRecords();


        PageDTO<LevelPrivilegeDTO> levelPrivilegeDTOPageDTO = levelConverter.levelPrivilegeDOToPageDTO(levelPrivilegeDOlist, total);
        return levelPrivilegeDTOPageDTO;
    }

    // 添加等级特权(后台等级权限管理-特权管理)
    @Override
    public void addLevelPrivilege(LevelPrivilegeCommand command) {
        if (command!=null) {
            LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
            levelPrivilegeMapper.insert(levelPrivilegeDO);
        }else {
            throw new BusinessException("添加角色不能为空", null);
        }


    }

    // 更新等级特权(后台等级权限管理-特权管理)
    @Override
    public void updateLevelPrivilege(LevelPrivilegeCommand command) {
        if (command!=null) {
            LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
            int i = levelPrivilegeMapper.updateById(levelPrivilegeDO);
            System.out.println("i = " + i);
        }else {
            throw new BusinessException("没有这个角色", null);
        }
    }

    // 更新等级特权(后台等级权限管理-特权管理)
    @Override
    public DeleteLevelPrivilegeOperateDTO deleteLevelPrivilege(Long id) {
        LevelPrivilegeDO levelPrivilegeDO = levelPrivilegeMapper.selectById(id);
        int deletNum = levelPrivilegeMapper.deleteById(id);
        System.out.println("deletNum = " + deletNum);
//        日志
        DeleteLevelPrivilegeOperateDTO deleteLevelPrivilegeOperateDTO = levelConverter.levelPrivilegeDOToDeleteLevelPrivilegeOperateDTO(levelPrivilegeDO);
        return deleteLevelPrivilegeOperateDTO;
    }

    // 批量删除更新等级特权(后台等级权限管理-特权管理)
    @Override
    public List<DeleteLevelPrivilegeOperateDTO> batchDeleteLevelPrivilege(BatchDeleteCommand command) {
        if (!command.getIds().isEmpty()) {
            List<String> ids = command.getIds();
            for (String id:ids) {
                levelPrivilegeMapper.deleteById(id);
            }
            QueryWrapper<LevelPrivilegeDO> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("is_deleted",0);
            List<LevelPrivilegeDO> levelPrivilegeDOS = levelPrivilegeMapper.selectList(queryWrapper);
            List<DeleteLevelPrivilegeOperateDTO> deleteLevelPrivilegeOperateDTOS = levelConverter.levelPrivilegeDOsToDeleteLevelPrivilegeOperateDTOs(levelPrivilegeDOS);
            return deleteLevelPrivilegeOperateDTOS;
        }
        return null;
    }
}
